package TMMiner;

import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.methods.interactive.Players;
import org.powerbot.game.api.methods.tab.Inventory;
import org.powerbot.game.api.util.Time;
import org.powerbot.game.api.wrappers.interactive.NPC;
import org.powerbot.game.api.wrappers.node.SceneObject;


public class Sleeping {

	/*
	 * sleeps until the bank interface is closed
	 */
	public static void waitForBankClosed() {
		if (Widgets.get(762).getChild(2).validate()) {
			for (int i = 0; i < 1000
					&& Widgets.get(762).getChild(2).validate(); i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * sleeps until the bank interface is opened
	 */
	public static void waitForBankOpened() {
		if (!Widgets.get(762).getChild(2).validate()) {
			for (int i = 0; i < 1000
					&& !Widgets.get(762).getChild(2).validate(); i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param bool The boolean to be waited for
	 */
	public static void waitForBoolean(final boolean bool) {
		if (!bool) {
			for (int i = 0; i < 1000 && !bool; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * sleeps until animation is -1, until player isn't moving, or both
	 */
	public static void waitForIdle() {
		if (Players.getLocal().getAnimation() !=-1) {
			for (int i = 0; i < 1000 && Players.getLocal().getAnimation() !=-1; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * sleeps until the inventory is completely empty
	 */
	public static void waitForInvEmpty() {
		if (Inventory.getCount() != 0) {
			for (int i = 0; i < 1000 && Inventory.getCount() != 0; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param item The item being waited on not be in inventory
	 */
	public static void waitForInvNotContain(final int item) {
		if (Inventory.getItem(item) !=null) {
			for (int i = 0; i < 1000 && Inventory.getCount(item) == 0; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param item The item to be waited for
	 * 
	 * @param amount The amount of the item to be waited for
	 */
	public static void waitForItem(final int item, final int amount) {
		if (Inventory.getCount(item) !=amount) {
			for (int i = 0; i < 1000 && Inventory.getCount(item) != amount; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param item The item to be waiting for to be de-selected
	 */
	public static void waitForNotSelected(final int item) {
		if (Inventory.getSelectedItem() !=null) {
			for (int i = 0; i < 1000 && Inventory.getSelectedItem() != null; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param npc The npc to be waiting to be on screen for
	 */
	public static void waitForNpc(final NPC npc) {
		if (!npc.isOnScreen()) {
			for (int i = 0; i < 1000 && !npc.isOnScreen(); i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param gameobject The GameObject to be waited for
	 * 
	 * @param condition True if wanting to wait on an object to be valid;
	 * otherwise false if wanting to wait on an object to be null
	 */
	public static void waitForObj(final SceneObject gameobject,
			final boolean condition) {
		if (!condition) {
			if (gameobject == null) {
				for (int i = 0; i < 1000 && gameobject == null; i++) {
					Time.sleep(10, 15);
				}
			}
		} else if (condition) {
			if (gameobject != null) {
				for (int i = 0; i < 1000 && gameobject != null; i++) {
					Time.sleep(10, 15);
				}
			}
		}
	}

	/*
	 * @param item The to be waiting for to be selected
	 */
	public static void waitForSelected(final int item) {
		if (Inventory.getSelectedItem() == null) {
			for (int i = 0; i < 1000 && Inventory.getSelectedItem() == null; i++) {
				Time.sleep(10, 15);
			}
		}
	}

	/*
	 * @param widget The id of the widget to be waited for
	 * 
	 * @param component The id of the component of the widget to be waited for
	 */
	public static void waitForWidget(final int widget, final int component) {
		if (!Widgets.get(widget).getChild(component).validate()) {
			for (int i = 0; i < 1000
					&& !Widgets.get(widget).getChild(component).validate(); i++) {
				Time.sleep(10, 15);
			}
		}
	}
}